require('./Getuserslist.styl');
const classNames = require('classnames')

const reactMixin = require('react-mixin');
import { List, Button, Scroller, NavBar, Context } from 'saltui'
import { Control } from 'react-keeper'
import { monStorage, Storage } from 'clientConfig/util/StoreData';
const RollUp = require('components/rollup');
class Getuserslist extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            active: false,
            listData: []
        };
        this.listData = [];//保存人员的列表数据
        this.handleUserCancle = this.handleUserCancle.bind(this);
        this.handleOnLeftClick = this.handleOnLeftClick.bind(this);
        this.refresh = this.refresh.bind(this);
    }
    handleClick(event, dataItem) {
        this.props.handleGetUsersList(dataItem)
    }
    handleOnLeftClick() {
        this.handleUserCancle();
    }
    refresh() {
        this.forceUpdate()
    }
    handleUserCancle() {
        let users = [];
        for (let [key, item] of this.state.listData.entries()) {
            let obj = this.refs[`roll${key}`];
            for (let [subkey, subitem] of item.dataList.entries()) {
                if (obj.props.MType != "C" && obj.state[`active${subkey}`]) {
                    users.push(subitem);
                } else {
                    if (obj.props.MType == "C") {
                        if (obj.state[`selectstudent${subkey}`] && obj.state[`selectstudent${subkey}`] instanceof Array && obj.state[`selectstudent${subkey}`].length > 0) {
                            let _obj = {};
                            let arr = obj.state[`selectstudent${subkey}`];
                            for (let item of arr) {
                                _obj.MType = item.MType;
                                _obj.talkIds = item.talkIds;
                                _obj.memberId = item.memberId;
                                users.push(_obj);
                                _obj = {};
                            }
                        }
                    }
                }
            }
        }
        this.props.handleUserCancle && this.props.handleUserCancle(users);
    }

    render() {
        let t = this;
        const { state: { active, listData }, handleUserCancle } = t;
        return (
            <div>
                <NavBar
                    className={classNames({"ios-nav-bar":window.isIphone})}
                    isShow={false}
                    onLeftClick={this.handleOnLeftClick}
                    onRightClick={Context.noop}
                    rightText=""
                    title="选择发布对象"
                />
                <Scroller ref="Scroller" className={classNames("getuserslist", { 'active': this.props.active })} >
                    {listData.length > 0 ? listData.map((item, index) => {
                        return (
                            <RollUp
                                data={item}
                                ref={`roll${index}`}
                                key={`roll${index}`}
                                refresh={this.refresh}
                                MType={item.MType}
                            />);
                    }) : <div className="t-FAC t-SMT100">暂无节点数据</div>}
                </Scroller>
            </div>

        );
    }

    componentWillMount() {
    }

    componentDidMount() {
        // debugger
        // if (!monStorage.get('org_list')) {
        //     if (this.listData && this.listData.length == 0) {
        //         this.props.handleGetUsersList && this.props.handleGetUsersList();
        //     }
        // } else {
        //     this.listData = monStorage.get('org_list');
        //     this.setState({ listData: this.listData })
        // }
        if (this.listData && this.listData.length == 0) {
            this.props.handleGetUsersList && this.props.handleGetUsersList();
        }
    }

    componentWillReceiveProps(nextProps) {

        if (nextProps.listData && nextProps.listData.length != 0 && nextProps.active) {
            this.listData = nextProps.listData;
            this.setState({ listData: this.listData }, () => {
            })
        }
        // if (this.state.active !== nextProps.active) {
        //     this.setState({ active: nextProps.active })
        // }

    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {

    }

    componentWillUnmount() {
        this.listData = []
    }
}


module.exports = Getuserslist;
